<?php
set_time_limit ( 0 );
include_once '../connect.php';

$sql = "SELECT id,zufang_url,shoujia,meipingzujin FROM fangjia where id >= 105570";
$result = mysql_query ( $sql, $dbc ) or die ( mysql_error () );

$Reg_yeshu = "/<div class=\"current\">1\/(.*)<\/div>/Us";
$Reg_url_page = "/<a href=\"(.*)\" title=\"下一页\">/U";
$Reg_rent_detial = "/<div id=\"prop_area_qt_(.*)\" class=\"areanum\">(.*).00m<span>2<\/span><\/div>(.*)<span>(.*)\/(.*)class=\"price\"><strong>(.*)<\/strong> 元\/月/Us";
while ( $arr_xiaoqu_zufang = mysql_fetch_array ( $result ) ) {
	if($arr_xiaoqu_zufang [zufang_url] == ''){// || $arr_xiaoqu_zufang[meipingzujin] == ''){
		continue;
	}
	$fcontents = file_get_contents ( $arr_xiaoqu_zufang [zufang_url] );
	preg_match_all ( $Reg_yeshu, $fcontents, $Arr_yeshu ); //$Arr_yeshu[1][0]是页数
	

	$arr_zujin_meiping = array ();
	
	//如果不只一页
	if ($Arr_yeshu [1] [0] > 1) {
		preg_match_all ( $Reg_url_page, $fcontents, $Arr_url_page ); //$Arr_url_page[1][0]是第二页，要页数的话去掉最后一个2
		$page_url = substr ( $Arr_url_page [1] [0], 0, strlen ( $Arr_url_page [1] [0] ) - 1 );
		//取得每页数据
		for($i = 1; $i <= $Arr_yeshu [1] [0]; ++ $i) {
			$fcontents = file_get_contents ( $page_url . $i );
			
			preg_match_all ( $Reg_rent_detial, $fcontents, $Arr_rent_detial );
			//$Arr_rent_detial[2][j]是平米，[4][j]里面判断有没有-来判断是不是地下，[6][j]是租金
			$Num = count ( $Arr_rent_detial [6] );
			for($j = 0; $j < $Num; ++ $j) {
				//找是不是地下室
				$domain = strstr ( $Arr_rent_detial [4] [$j], '-' );
				if ($domain != FALSE) {
					continue;
				}
				//租金是不是低于1000
				if ($Arr_rent_detial [6] [$j] < 1000) {
					continue;
				}
				if($Arr_rent_detial [2] [$j] == 0){
					continue;
				}
				$zujin_meiping = $Arr_rent_detial [6] [$j] / $Arr_rent_detial [2] [$j];
				if ($zujin_meiping < 15|| $zujin_meiping > 150) {
					continue;
				}
				$arr_zujin_meiping [] = $zujin_meiping;
			}
		}
		$Num_arr = count ( $arr_zujin_meiping );
		$sum = 0;
		for($k = 0; $k < $Num_arr; ++ $k) {
			$sum += $arr_zujin_meiping [$k];
		}
		$average = $sum / $Num_arr;
	
	} 
	
	elseif ($Arr_yeshu [1] [0] == 1) {
		preg_match_all ( $Reg_rent_detial, $fcontents, $Arr_rent_detial );
		//$Arr_rent_detial[2][j]是平米，[4][j]里面判断有没有-来判断是不是地下，[6][j]是租金
		$Num = count ( $Arr_rent_detial [6] );
		for($j = 0; $j < $Num; ++ $j) {
			//找是不是地下室
			$domain = strstr ( $Arr_rent_detial [4] [$j], '-' );
			if ($domain != FALSE) {
				continue;
			}
			//租金是不是低于1000
			if ($Arr_rent_detial [6] [$j] < 1000) {
				continue;
			}
			$zujin_meiping = $Arr_rent_detial [6] [$j] / $Arr_rent_detial [2] [$j];
			if ($zujin_meiping < 15 || $zujin_meiping > 150) {
				continue;
			}
			$arr_zujin_meiping [] = $zujin_meiping;
		}
		$Num_arr = count ( $arr_zujin_meiping );
		$sum = 0;
		for($k = 0; $k < $Num_arr; ++ $k) {
			$sum += $arr_zujin_meiping [$k];
		}
		$average = $sum / $Num_arr;
	}
	
	$zushoubi = $arr_xiaoqu_zufang [shoujia] / $average;
	$query = "UPDATE fangjia SET meipingzujin = '$average',zushoubi='$zushoubi' WHERE id = '$arr_xiaoqu_zufang[id]'";
	
	$result_over = mysql_query ( $query, $dbc ) or die ( mysql_error () );
	echo $arr_xiaoqu_zufang[id]."<br>";

}

echo "over le";

?>